package com.dm.manage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dm.manage.common.Result;
import com.dm.manage.entity.Student;
import com.dm.manage.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/student")
@RequiredArgsConstructor
public class StudentController {

    private final StudentService studentService;
    @GetMapping("info/{id}")
    public Result<Student> getInfo(@PathVariable("id") Integer id) {
        return Result.success(studentService.getById(id));
    }

    @PostMapping("addInfo")
    public Result<String> addInfo(@RequestBody Student student) {
        // 对于AUTO模式，不设置ID，让数据库自动生成
        if (student.getId() != null && student.getId() <= 0) {
            student.setId(null);
        }
        studentService.save(student);
        return Result.success("保存学生信息成功");
    }
    @PostMapping("updateInfo")
    public Result<String> updateInfo(@RequestBody Student student) {
        studentService.updateById(student);
        return Result.success("更新学生信息成功");
    }

    @PostMapping("deleteInfo/{id}")
    public Result<String> deleteInfo(@PathVariable Integer id ) {
        studentService.removeById(id);
        return Result.success("删除学生信息成功");
    }
    @GetMapping("list")
    public Result<Page<Student>> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(studentService.listPages(pageNum, pageSize));
    }
}